package org.ccams.tran;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.ccams.domain.TSalesordersD;
import org.ccams.util.Utils;
import org.json.JSONArray;

public class Tran151180 extends AbstractJsonTran {
	
	@SuppressWarnings("unchecked")
	public void buildJSONObject(Map model, JSONObject json,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String params = (String) request.getParameter("salesordersd");
		JSONArray salesordersd = new JSONArray(params);
		
		System.out.println("调试数据："+salesordersd);
		
		HashMap<String, Object> jsonMap = new HashMap<String, Object>();
		boolean success = false;
		String message = "";
		JSONArray newSalesordersd = new JSONArray();
		int k = 0;
		for(int i=0;i<salesordersd.length();i++){
			org.json.JSONObject ordersd = salesordersd.optJSONObject(i);
			if(ordersd.has("sale_price")){
				TSalesordersD t = new TSalesordersD();
				t.setIdt_salesorders_h(1);
				t.setLine_id(new Long(i+1));
				t.setGoodsid(new Long(1));
				t.setRequest_date(ordersd.getString("request_date"));
				t.setSchedule_ship_date(ordersd.getString("schedule_ship_date"));
				t.setSchedule_arrival_date(ordersd.getString("schedule_arrival_date"));
				//获得当期时间
				t.setOrder_date(Utils.getCurDate().toString());
				t.setGoodsname(ordersd.getString("goodsname"));
				t.setSpec(ordersd.getString("spec"));
				t.setSale_price(new BigDecimal(ordersd.getString("sale_price")));
				t.setOrder_quantity_uom(new BigDecimal(ordersd.getString("order_quantity_uom")));
				t.setFlow_status_code(ordersd.getString("flow_status_code"));
				t.setAttrib1(new BigDecimal(ordersd.getString("attrib1")));
				t.setAttrib1_unit(ordersd.getString("attrib1_unit"));
				t.setAttrib2(new BigDecimal(ordersd.getString("attrib2")));
				t.setAttrib2_unit(ordersd.getString("attrib2_unit"));
				t.setAttrib3(new BigDecimal(ordersd.getString("attrib3")));
				t.setAttrib3_unit(ordersd.getString("attrib3_unit"));
				t.setAttrib4(new BigDecimal(ordersd.getString("attrib4")));
				t.setAttrib4_unit(ordersd.getString("attrib4_unit"));
				t.setAttrib5(new BigDecimal(ordersd.getString("attrib5")));
				t.setAttrib5_unit(ordersd.getString("attrib5_unit"));
				
				System.out.println("for循环执行更新次数为："+i);
//				k = getCcamsService().getTsalesordersdDao().updateByPrimaryKey(t);	//更新
				k = getCcamsService().getTsalesordersdDao().insertSalesordersd(t);
			}else{
				newSalesordersd.put(ordersd);
			}			
		}
		for(int i=0;i<newSalesordersd.length();i++){
			org.json.JSONObject newOrdersd = salesordersd.optJSONObject(i);
			TSalesordersD t = new TSalesordersD();
			t.setIdt_salesorders_h(1);
			t.setLine_id(new Long(i+1));
			t.setGoodsid(new Long(1));
			
			t.setRequest_date(newOrdersd.getString("request_date"));
			t.setSchedule_ship_date(newOrdersd.getString("schedule_ship_date"));
			t.setSchedule_arrival_date(newOrdersd.getString("schedule_arrival_date"));
			//获得当期时间
			t.setOrder_date(Utils.getCurDate().toString());
			t.setGoodsid(newOrdersd.getLong("goodid"));
			t.setGoodsname(newOrdersd.getString("goodsname"));
			t.setSpec(newOrdersd.getString("spec"));
			t.setSale_price(new BigDecimal(newOrdersd.getString("sale_price")));
			t.setOrder_quantity_uom(new BigDecimal(newOrdersd.getString("order_quantity_uom")));
			t.setFlow_status_code(newOrdersd.getString("flow_status_code"));
			t.setAttrib1(new BigDecimal(newOrdersd.getString("attrib1")));
			t.setAttrib1_unit(newOrdersd.getString("attrib1_unit"));
			t.setAttrib2(new BigDecimal(newOrdersd.getString("attrib2")));
			t.setAttrib2_unit(newOrdersd.getString("attrib2_unit"));
			t.setAttrib3(new BigDecimal(newOrdersd.getString("attrib3")));
			t.setAttrib3_unit(newOrdersd.getString("attrib3_unit"));
			t.setAttrib4(new BigDecimal(newOrdersd.getString("attrib4")));
			t.setAttrib4_unit(newOrdersd.getString("attrib4_unit"));
			t.setAttrib5(new BigDecimal(newOrdersd.getString("attrib5")));
			t.setAttrib5_unit(newOrdersd.getString("attrib5_unit"));  
			
			System.out.println("for循环执插入行次数为："+i);
			k = getCcamsService().getTsalesordersdDao().updateByPrimaryKey(t);	//更新
//			k = getCcamsService().getTsalesordersdDao().insertSalesordersd(t);
		}
		
		System.out.println("返回结果为:"+k);
		
		if (k == 0) {
			success = false;
			message = "销售明细保存失败";
		} else {
			success = true;
			message = "销售明细保存成功";
		}
		jsonMap.put("success", success);
		jsonMap.put("message", message);
		json.putAll(jsonMap);
	}
}
